﻿using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class PathPlotter : MonoBehaviour 
{
    public Vector3[] Points;
    public Color PlotColor = Color.yellow;

    public float Speed = 1;
    public float Tolerance = .1f;

    int currentTarget = 0;

	// Use this for initialization
	void Start () 
    {
	
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (Application.isEditor)
        {
            int pcnt = Points.Length;
            Vector3 lastPos = Vector3.zero;
            Vector3 nextPos = Vector3.zero;

            for (int p = 0; p < pcnt; p++)
            {
                nextPos = Points[p];
                if (p > 0)
                    Debug.DrawLine(lastPos, nextPos, PlotColor);

                lastPos = nextPos;
            }
        }
        if(Application.isPlaying)
        {
            if (Points != null && Points.Length > 0)
            {
                transform.position = Vector3.Lerp(transform.position, Points[currentTarget], Time.deltaTime * Speed);

                if (Vector3.Distance(transform.position , Points[currentTarget]) <= Tolerance)
                {
                    currentTarget++;
                    if (currentTarget >= Points.Length)
                        currentTarget = 0;
                }
            }
        }
	}
    
}
